package com.aglhz.nature.presenter;

import java.util.List;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import android.util.Log;

import com.aglhz.nature.events.ShopList2ViewEvent;
import com.aglhz.nature.events.ShopListViewEvent;
import com.aglhz.nature.modle.ShopListBean;
import com.aglhz.nature.modle.item.ShopListData;
import com.aglhz.nature.presenter.base.BasePresenter;
import com.aglhz.nature.view.iv.ShopList2View;
import com.aglhz.nature.view.iv.ShopListView;
import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.TextHttpResponseHandler;

public class ShopList2Presenter extends BasePresenter{
	
	private static String TAG =ShopList2Presenter.class.getName();
	// view
		private ShopList2View shopList2View;
		// data
		private ShopListBean shopListBean;
		private List<ShopListData> data;
		public ShopList2Presenter(ShopList2View view) {
			super();
			shopList2View = view;
		}
		
		
		public void loadData(){
			String categoryId=shopList2View.getId();
			this.loadShopListBean(categoryId);
		}
		
		
		
		
		private void showListView() {
			shopList2View.showList(shopListBean.getData());
			
		}
		
		
		
		@Subscribe(threadMode = ThreadMode.MAIN)
		public void onEventShopList2Thread(ShopList2ViewEvent event) {

			String msg = event.getMessage();
			Log.i(TAG, " ShopList2ViewEvent:" + msg);
			// update UI By event
			if ( ShopList2ViewEvent.EVENT_UPDATE_SHOP2_LISTVIEW == msg) {
				// update grid view
				this.showListView();
			} 
			

		}
		
		
		
		
		
		public void loadShopListBean(String categoryId){
			String url = "http://h5.aglhz.com/gc_community/member/search/searchGoodsList.do?categoryId="+categoryId;
			//http://h5.aglhz.com/gc_community/member/search/searchGoodsList.do?categoryId=ba20e2f0-3f14-4996-be95-3f03ecbea9e8
			Log.e("---------url---",url);
			AsyncHttpClient client = new AsyncHttpClient();
			client.get(url, new TextHttpResponseHandler() {
				;
				@Override
				public void onSuccess(int statusCode,
						cz.msebera.android.httpclient.Header[] headers,
						String responseString) {
					 try {
						 Log.e(TAG, "response" + responseString);
						
						 Gson gson = new Gson();
						
						 ShopListBean slBean = gson.fromJson(responseString,
								 ShopListBean.class);
						 shopListBean= (ShopListBean) slBean.clone();
						
						Log.d(TAG,"ShopListView---111-----"+shopListBean.getOther());	
						data = shopListBean.getData();
						Log.d(TAG,"ShopListView---mcp-----"+shopListBean.getData());	
						 // send event
						ShopList2ViewEvent event = new  ShopList2ViewEvent(
								ShopList2ViewEvent.EVENT_UPDATE_SHOP2_LISTVIEW);
						 EventBus.getDefault().post(event);
						 
						 Log.e(TAG, "success" + "success");
						 } catch (Exception e) {
						 e.printStackTrace();
						 }
					
					
				}
				
				@Override
				public void onFailure(int statusCode,
						cz.msebera.android.httpclient.Header[] headers,
						String responseString, Throwable throwable) {
					
					 Log.e(TAG, "failure");
				}
			

		

		
			});
		}
	
		
}